// pages/coupon/coupon.js
const app = getApp();
var formatTime = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponList:[], //优惠券列表
    iNone: false,
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
  },

  //初始化数据
  init(){
    wx.request({
      url: app.baseURL + 'Index&a=coupon_list',
      data: {
        user_id: app.globalData.userId
      },
      success: (res) => {
        if (res.data.status == 1) {
          let data = res.data;
          this.format(data.yhq_info, 'start_time');
          this.format(data.yhq_info, 'end_time');
          // this.format(data.user_yhq, 'start_time');
          // this.format(data.user_yhq, 'end_time');
          // this.format(data.gq_yhq, 'start_time');
          // this.format(data.gq_yhq, 'end_time');
          // this.format(data.yhq_fin, 'start_time');
          // this.format(data.yhq_fin, 'end_time');
          this.format(data.jf_yhq_info, 'start_time');
          this.format(data.jf_yhq_info, 'end_time');
          this.setData({
            jflingqu: data.jf_yhq_info, //积分兑换优惠券
            // jflingqued: data.jf_yhq_fin, //积分兑换优惠券
            // yilingqu: data.user_yhq, //已领取的优惠券
            // yiguoqi: data.gq_yhq, //已过期的优惠券
            weilingqu: data.yhq_info, //未领取的优惠券
            // yilingwan: data.yhq_fin, //已领完的优惠券
            isNone: !data.yhq_info.length && !data.jf_yhq_info.length          
          })
        } else if (res.data.status == 0){
          this.setData({
            jflingqu: [], //积分兑换优惠券
            // yilingqu: [], //已领取的优惠券
            // yiguoqi: [], //已过期的优惠券
            weilingqu: [], //未领取的优惠券
            // yilingwan: [], //已领完的优惠券
            isNone: true
          })
        };


        // 如果有回调函数执行回调
        typeof cb == 'function' && cb();
      }
    })
  },

  //将时间戳转换成日期
  format(arr, prop){
    arr.forEach((item) => {
      item[prop] = formatTime.formatTime(item[prop] * 1000, 'hms');
    });
  },

  //领取优惠券
  getCoupon(e){
    let list = this.data.couponList;
    const yhq_id = e.currentTarget.dataset.id; //优惠券id
    wx.request({
      url: app.baseURL + 'Index&a=coupon_get',
      data: {
        user_id: app.globalData.userId,
        yhq_id: yhq_id
      },
      success: (res) => {
        if (res.data.status == 1) {
          wx.showToast({ title: '领取成功', icon: 'none' });
          this.init();
        }
      }
    });
  },

  //兑换优惠券
  getIntegral(e) {
    let tath=this;
    let list = this.data.couponList;
    const yhq_id = e.currentTarget.dataset.id; //优惠券id
    wx.request({
      url: app.baseURL + 'Index&a=change',
      data: {
        user_id: app.globalData.userId,
        yhq_id: yhq_id
      },
      success: (res) => {
        if (res.data.status == 1) {
          wx.showModal({
            title: '',
            content: res.data.info,
            success: function (res) {
              if (res.confirm) {
                wx.request({
                  url: app.baseURL + 'Index&a=coupon_get',
                  data: {
                    user_id: app.globalData.userId,
                    yhq_id: yhq_id,
                    ask: 1
                  },
                  success: (res) => {
                    tath.init();
                    wx.showToast({
                      title: '领取成功',
                      icon: 'none',
                    })
                  }
                });
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          // 
        } else if (res.data.status == 0) {
          wx.showToast({
            title: '您的积分不足',
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  }
})